/*
 * Copyright (C) 2008 Alessandro Sivieri <alessandro.sivieri@chimera-bellerofonte.eu>.
 *
 *  This file is part of Daex.
 *
 *  Daex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.chimera_bellerofonte.tesi.ui.models;

import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import eu.chimera_bellerofonte.tesi.DaexLogger;
import eu.chimera_bellerofonte.tesi.datatypes.Song;


/**
 * Implementazione di default per il sistema Daex. Utilizza le
 * liste e le tabelle dichiarate per eseguire il dnd.
 * 
 * @author Sivieri Alessandro
 * @author Vitucci Nicola
 * @version 14/mag/07
 */
public class DaexSongTransferHandler extends SongTransferHandler
{

	static final long serialVersionUID = 1L;

	/**
	 * @see eu.chimera_bellerofonte.tesi.ui.models.SongTransferHandler#getTransferable(javax.swing.JComponent)
	 */
	@Override
	protected SongTransferable getTransferable(JComponent c)
	{
		int i=((JTable) c).getSelectedRowCount();
		if(i==0)
		{
			DaexLogger.getDaexLogger().severe("Si sta iniziando un trasferimento senza avere elementi selezionati!");
			return null;
		}
		Song[] s=new Song[i];
		int k=0;
		for(int j: ((JTable)c).getSelectedRows())
		{
			s[k]=(Song) ((AbstractTableModel) ((JTable) c).getModel()).getValueAt(j, DaexTable.OBJECTCOLUMNINDEX);
			k++;
		}
		return new SongTransferable(s);
	}

	/**
	 * @see eu.chimera_bellerofonte.tesi.ui.models.SongTransferHandler#setSongs(JComponent, Song[])
	 */
	@Override
	protected void setSongs(JComponent c, Song[] s)
	{
		for(Song ss: s)
			((SortedListModel) ((JList) c).getModel()).addElement(ss);
		((SortedListModel) ((JList) c).getModel()).sort();
	}

}
